#!/usr/bin/perl
# Copyright (C) 2011-2013 Zentyal S.L.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

use warnings;
use strict;

use Error qw(:try);
use File::Basename;
use File::Slurp;
use Cwd;

my ($DESTDIR) = @ARGV;
unless ($DESTDIR) {
    $DESTDIR = '/';
}

my $MODULE_NAME = basename(getcwd());
$MODULE_NAME =~ s/^zentyal-//;
$MODULE_NAME =~ s/-[0-9].*$//;

if (exists $ENV{MODULE_NAME}) {
    $MODULE_NAME = $ENV{MODULE_NAME};
}

my %DIR;
$DIR{PERL} = "${DESTDIR}usr/share/perl5";
$DIR{SCHEMAS} = "${DESTDIR}usr/share/zentyal/modules";
$DIR{DATA} = "${DESTDIR}usr/share/zentyal-$MODULE_NAME";
$DIR{STUBS} = "${DESTDIR}usr/share/zentyal/stubs/$MODULE_NAME";
$DIR{TEMPLATES} = "${DESTDIR}usr/share/zentyal/templates/$MODULE_NAME";
$DIR{ETC} = "${DESTDIR}etc/zentyal";
$DIR{MIGRATION} = "${DESTDIR}usr/share/zentyal-$MODULE_NAME/migration";
$DIR{SQL} = "${DESTDIR}usr/share/zentyal-$MODULE_NAME/sql";
$DIR{URLS} = "${DESTDIR}usr/share/zentyal/urls";
$DIR{WWW} = "${DESTDIR}usr/share/zentyal/www";
$DIR{CGI} = "${DESTDIR}usr/share/zentyal/cgi";
$DIR{PSGI} = "${DESTDIR}usr/share/zentyal/psgi";

my %srcs;

# Special cases for paths
if (($MODULE_NAME eq 'common') or ($MODULE_NAME eq 'core')) {
    $DIR{DATA} = "${DESTDIR}usr/share/zentyal";
    $DIR{MIGRATION} = "${DESTDIR}usr/share/zentyal/migration";
    $DIR{TEMPLATES} = "${DESTDIR}usr/share/zentyal/templates";

    add_if_exists('src', $DIR{PERL}, '*.pm');
}

add_if_exists('schemas', $DIR{SCHEMAS}, '*.yaml');
add_if_exists('schemas', $DIR{DATA}, '*.ldif');
add_if_exists('schemas/urls', $DIR{URLS}, '*.urls');
add_if_exists('schemas/sql', $DIR{DATA});
add_if_exists('www', $DIR{WWW}, '*');
add_if_exists('src/EBox', $DIR{PERL});
add_if_exists('src/Zentyal', $DIR{PERL});
add_if_exists('src/scripts', $DIR{DATA}, '*');
add_if_exists('src/templates', $DIR{TEMPLATES}, '*');
add_if_exists('src/cgi', $DIR{CGI}, '*.cgi');
add_if_exists('src/psgi', $DIR{PSGI}, '*.psgi');
add_if_exists('stubs', $DIR{STUBS}, '*');
add_if_exists('conf', $DIR{ETC}, '*');
add_if_exists('migration', $DIR{MIGRATION}, '*');

# Add extra files to custom paths
if (-d 'extra') {
    my @lines = read_file("extra/files.list");
    chomp (@lines);
    foreach my $line (@lines) {
        my ($file, $destdir) = split (' ', $line);
        $srcs{"extra/$file"} = "${DESTDIR}$destdir";
    }
}

my %dirs = map { $_ => 1 } values %srcs;
foreach my $dir (keys %dirs) {
    system ("mkdir -p $dir");
}
foreach my $src (keys %srcs) {
    my $destdir = $srcs{$src};
    system ("cp -r $src $destdir/");

    # Replace @VERSION@ variable in Config.pm
    # TODO: Maybe this could be implemented in other way or even removed
    #       if we get rid of all the Exporter stuff where it is used
    if ($src eq './src/EBox/Config.pm') {
        my $version = read_version();
        my $filename = basename($src);
        system ("sed -i 's/\@VERSION@/$version/' $destdir/$filename");
    }
}

# Helper functions

sub add_if_exists
{
    my ($src, $dst, $what) = @_;
    $what = '' unless defined ($what);

    return if filter($src);

    if (-d $src) {
        $srcs{"$src/$what"} = $dst;
    }
}

sub filter
{
    my ($src) = @_;

    # Skip unit tests
    if (($src =~ m{/t/}) or ($src =~ m{/Test\.pm$})) {
        return 1;
    }

    return 0;
}

sub read_version
{
    my @lines = read_file('ChangeLog');
    for my $line (@lines) {
        my ($version) = $line =~ /^([\d\.]+)/;
        if ($version) {
            return $version;
        }
    }
}
